package com.dlc.shop.live.common.listener;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dlc.shop.bean.enums.ProdStatusEnums;
import com.dlc.shop.bean.event.ProdChangeEvent;
import com.dlc.shop.bean.event.ProdChangeStatusEvent;
import com.dlc.shop.bean.model.Product;
import com.dlc.shop.bean.order.GeneralActivitiesOrder;
import com.dlc.shop.common.constants.CacheNames;
import com.dlc.shop.common.util.Json;
import com.dlc.shop.common.util.RedisUtil;
import com.dlc.shop.common.wukongim.constant.WuKongConstant;
import com.dlc.shop.live.common.constant.LiveCacheNames;
import com.dlc.shop.live.common.dao.LiveRoomProdMapper;
import com.dlc.shop.live.common.model.LiveRoomProd;
import com.dlc.shop.live.common.service.LiveRoomProdService;
import com.dlc.shop.live.common.vo.LiveMsgItem;
import com.dlc.shop.wukongim.service.WuKongImService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 直播商品状态修改监听
 * @author TRACK
 */
@Component("liveProdChangeStatus")
@AllArgsConstructor
public class ProdChangeStatusListener {

    private final LiveRoomProdMapper liveRoomProdMapper;
    private final LiveRoomProdService liveRoomProdService;
    private final WuKongImService wuKongImService;

    @EventListener(ProdChangeStatusEvent.class)
    @Order(GeneralActivitiesOrder.LIVE)
    public void liveProdChangeStatusListener(ProdChangeStatusEvent event) {
        Product product = event.getProduct();

        List<Long> roomIds = liveRoomProdMapper.listRoomIdByProdId(product.getProdId());
        if (CollUtil.isEmpty(roomIds)) {
            // 没有参与直播
            return;
        }

        // 如果是商品上线，就不管他了
        if (Objects.equals(ProdStatusEnums.NORMAL.getValue(), event.getStatus())) {
            return;
        }

        // 平台审核
        if (Objects.equals(ProdStatusEnums.PLATFORM_AUDIT.getValue(), event.getStatus())) {
            return;
        }

        delete(product, roomIds);
    }

    private void delete(Product product, List<Long> roomIds) {
        // 从直播间删除商品
        liveRoomProdMapper.delete(new LambdaQueryWrapper<LiveRoomProd>()
                .eq(LiveRoomProd::getProdId, product.getProdId())
                .in(LiveRoomProd::getRoomId, roomIds));
        // 清除缓存
        List<String> keys = new ArrayList<>();
        for (Long roomId : roomIds) {
            keys.add(LiveCacheNames.LIVE_ROOM_PROD + CacheNames.UNION + roomId);
            LiveMsgItem prodMsg = new LiveMsgItem(WuKongConstant.PROD_ERROR, "商品状态异常");
            wuKongImService.sendLiveRoomMessage(WuKongConstant.LIVE_CHANNEL_PREFIX + roomId, Json.toJsonString(prodMsg), null);
        }
        RedisUtil.del(keys);
    }

    @EventListener(ProdChangeEvent.class)
    @Order(GeneralActivitiesOrder.LIVE)
    public void liveProdDeleteListener(ProdChangeEvent event) {
        Product product = event.getProduct();

        List<Long> roomIds = liveRoomProdMapper.listRoomIdByProdId(product.getProdId());
        if (CollUtil.isEmpty(roomIds)) {
            // 没有参与直播
            return;
        }

        // 从直播间删除商品
        delete(product, roomIds);
    }
}
